package com.framework.core;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.inputmethod.InputMethodManager;

import com.framework.core.widget.LoadingDialog;

/**
 * @author shenmengchao
 * @version 1.0.0
 * @date 2017/7/18
 * @description
 */

public abstract class BaseActivity extends AppCompatActivity {

    private final static String TAG = BaseActivity.class.getSimpleName();

    private Dialog mLoadingDialog;

    public void showLoadingDialog() {
        showLoadingDialog(true, R.string.default_loading_message);
    }

    public void showLoadingDialog(int msgId) {
        showLoadingDialog(true, msgId);
    }

    public void showLockableLoadingDialog() {
        showLoadingDialog(false, R.string.default_loading_message);
    }

    public void showLockableLoadingDialog(int msgId) {
        showLoadingDialog(false, msgId);
    }


    public void showLoadingDialog(boolean isCancelable, int msgId) {
        if (null == mLoadingDialog) {
            mLoadingDialog = new LoadingDialog(this, isCancelable, msgId);
        }
        if (mLoadingDialog != null && !mLoadingDialog.isShowing()) {
            mLoadingDialog.show();
        }
    }

    public void dismissLoadingDialog() {
        try {
            if (null != mLoadingDialog && mLoadingDialog.isShowing()) {
                mLoadingDialog.dismiss();
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (null != this && null != getCurrentFocus()) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); // 强制隐藏键盘
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        return super.dispatchTouchEvent(event);
    }
}
